package com.msb.apidriver.service;

import com.msb.apidriver.feign.ServiceDriverUserClient;
import com.msb.apidriver.feign.ServiceVerificationCodeClient;
import com.msb.commons.constant.CommonStatusEnum;
import com.msb.commons.constant.DriverUserExistsConstant;
import com.msb.commons.constant.IdentityConstant;
import com.msb.commons.constant.TokenConstant;
import com.msb.commons.dto.VerificationCodeDTO;
import com.msb.commons.response.DriverUserExistsResponse;
import com.msb.commons.response.NumberCodeResponse;
import com.msb.commons.response.ResponseResult;
import com.msb.commons.response.TokenResponse;
import com.msb.commons.util.JwtUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author: chshh
 * @version: 1.0
 */
@Service
public class VerificationCodeService {
    @Autowired
    private ServiceDriverUserClient driverUserClient;
    @Autowired
    private ServiceVerificationCodeClient serviceVerificationCodeClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public ResponseResult checkAndSendVerificationCode(String driverPhone){
        //查询service-driver-user,该手机号的司机是否存在
        ResponseResult<DriverUserExistsResponse> driverUser = driverUserClient.getDriverUser(driverPhone);
        DriverUserExistsResponse data = driverUser.getData();
        Integer ifExists = data.getIfExists();
        if (ifExists == DriverUserExistsConstant.IFEXISTS_NO){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_NOT_EXISTS.getValue());
        }

        //获取验证码
        ResponseResult<NumberCodeResponse> numberCodeResponseResponseResult = serviceVerificationCodeClient.numberCode();
        int numberCode = numberCodeResponseResponseResult.getData().getNumberCode();

        //调用第三方发送验证码

        //存入redis
        stringRedisTemplate.opsForValue().set(IdentityConstant.DRIVER+driverPhone,String.valueOf(numberCode),2, TimeUnit.MINUTES);
        return ResponseResult.success("");
    }

    /**
     * 校验验证码
     * @param driverPhone
     * @param verificationCode
     * @return
     */
    public ResponseResult generatorCodeCheck(String driverPhone,String verificationCode){

        //1.根据手机号读取验证码
        String codeRedis = stringRedisTemplate.opsForValue().get(IdentityConstant.DRIVER + driverPhone);
        System.out.println("redis中的value:"+codeRedis);
        //2.校验验证码
        if(StringUtils.isBlank(codeRedis)){ //判断是否为空,是就返回true
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        if(!verificationCode.trim().equals(codeRedis.trim())){ //trim() 方法用于删除字符串的头尾空白符，空白符包括：空格、制表符 tab、换行符等其他空白符等。
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }

        //4.颁发令牌
        String accessToken = JwtUtils.generatorToken(driverPhone, IdentityConstant.DRIVER, TokenConstant.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(driverPhone, IdentityConstant.DRIVER, TokenConstant.REFRESH_TOKEN_TYPE);

        //5.将token存到redis中
        String accessTokenKey = TokenConstant.ACCESS_TOKEN_TYPE+driverPhone+IdentityConstant.DRIVER;

        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);

        String refreshTokenKey = TokenConstant.REFRESH_TOKEN_TYPE+driverPhone+IdentityConstant.DRIVER;
        //控制时间交给服务端控制后,生成token时不能设置过期时间
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);
        //6.响应
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }
}